﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MyLibrary.IO.TextParser;

namespace FezLogForSns
{
	class AppConfig
	{

		#region privateなメンバ

		/// <summary>
		/// コンフィグパーサ
		/// </summary>
		private ConfigParser iniConfig;

		/// <summary>
		/// 新規フラグ
		/// </summary>
		private bool isNewFlg = false;

		#endregion

		#region 設定項目

		/// <summary>
		/// 出力先
		/// </summary>
		public string convertTarget;

		/// <summary>
		/// エクスポート先ディレクトリ
		/// </summary>
        public string exportDirectory;

        public Dictionary<string, bool> checkStatus;
        public Dictionary<string, bool> checkBold;
        public Dictionary<string, bool> checkItalic;
        public Dictionary<string, string> colorCode;

		#endregion


		/// <summary>
		/// コンストラクタ
		/// </summary>
		public AppConfig()
		{
            string[] range = Constant.Range;
            checkStatus = new Dictionary<string, bool>();
            checkBold = new Dictionary<string, bool>();
            checkItalic = new Dictionary<string, bool>();
            colorCode = new Dictionary<string, string>();

			ConfigParser config = new ConfigParser(Constant.ConfigFileName, Encoding.GetEncoding("Shift_JIS"));
			try
			{
                foreach (string type in range)
                {
                    checkStatus[type] = bool.Parse(config["enable"][type].Value);
                    checkBold[type] = bool.Parse(config["bold"][type].Value);
                    checkItalic[type] = bool.Parse(config["italic"][type].Value);

                    string color = config["color"][type].Value;

                    if(!AppUtil.isValidColorCode(color)){
                        throw new Exception();
                    }
                    colorCode[type] = color;
                }

                convertTarget = config["base"]["convertTarget"].Value;
                exportDirectory = config["base"]["exportDirectory"].Value;
			}
			catch (Exception)
			{
                isNewFlg = true;

                foreach (string type in range)
                {
                    checkStatus[type] = true;
                    checkBold[type] = false;
                    checkItalic[type] = false;
                }

                colorCode["say"] = "000000";
                colorCode["all"] = "FF0000";
                colorCode["armySay"] = "FF9900";
                colorCode["army"] = "E9C402";
                colorCode["tell"] = "FF00FF";
                colorCode["party"] = "0000FF";
                colorCode["force"] = "31CD31";
                colorCode["emotion"] = "BE8D5D";
                colorCode["system"] = "BE8D5D";

                convertTarget = "1";
				exportDirectory = "";
			}

			iniConfig = config;
		}

		/// <summary>
		/// 現在の設定を保存します。
		/// 
		/// TODO 例外処理
		/// </summary>
		public bool save()
		{
            ConfigParser config = iniConfig;
            string[] range = Constant.Range;

			// 変更されているかチェック
			if (!isNewFlg && !isChanged()){
				return false;
			}

            foreach (string type in range)
            {
                config["enable"][type].Value = checkStatus[type].ToString();
                config["bold"][type].Value = checkBold[type].ToString();
                config["italic"][type].Value = checkItalic[type].ToString();
                config["color"][type].Value = colorCode[type];
            }

            config["base"]["exportDirectory"].Value = exportDirectory;
            config["base"]["convertTarget"].Value = convertTarget;
			config.Save();
			return true;
		}

        private bool isChanged()
        {
            ConfigParser config = iniConfig;
            string[] range = Constant.Range;

            foreach (string type in range)
            {
                if (checkStatus[type] != bool.Parse(config["enable"][type].Value)) return true;
                if (checkBold[type] != bool.Parse(config["bold"][type].Value)) return true;
                if (checkItalic[type] != bool.Parse(config["italic"][type].Value)) return true;
                if (colorCode[type] != config["color"][type].Value) return true;
            }

            if (config["base"]["exportDirectory"].Value != exportDirectory)
            {
                return true;
            }

            if (config["base"]["convertTarget"].Value != convertTarget)
            {
                return true;
            }

            return false;
        }

	}
}
